/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplikasi.com.implement;

import aplikasi.com.entity.pelanggan;
import aplikasi.com.interfac.interPelanggan;
import aplikasi.com.koneksiDatabase.DatabaseUtilities;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Henfryandie
 */
public class implemPelanggan implements interPelanggan {

    public pelanggan insertKar(pelanggan o) throws SQLException {
        String sqlInsert = "INSERT INTO aplikasirentakkomik.pelanggan(namaPenyewa,alamat,noTelp,noIdentitas) VALUES (?,?,?,?);";
        PreparedStatement prepare = DatabaseUtilities.getConnection().prepareStatement(sqlInsert);
        prepare.setString(1, o.getNamaPelanggan());
        prepare.setString(2, o.getAlamat());
        prepare.setString(3, o.getNoTelp());
        prepare.setString(4, o.getNoIdentitas());
        prepare.executeUpdate();
        return o;
    }

    public void update(pelanggan o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(String nama) throws SQLException {
        String sql = "DELETE FROM aplikasirentakkomik.pelanggan WHERE namaPenyewa=?;";
        PreparedStatement prepared = DatabaseUtilities.getConnection().prepareStatement(sql);
        prepared.setString(1, nama);
        prepared.executeUpdate();
    }

    public List<pelanggan> getAll() throws SQLException {
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("select * from pelanggan");
        List<pelanggan> list = new ArrayList<pelanggan>();
        while (rs.next()) {
            pelanggan o = new pelanggan();
            o.setIdPelanggan(rs.getInt("idPelanggan"));
            o.setNamaPelanggan(rs.getString("namaPenyewa"));
            o.setAlamat(rs.getString("alamat"));
            o.setNoTelp(rs.getString("noTelp"));
            o.setNoIdentitas(rs.getString("noIdentitas"));
            list.add(o);
        }
        return list;
    }

    public List<pelanggan> viewPelanggan() throws SQLException {
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("select idPelanggan from pelanggan");
        List<pelanggan> list = new ArrayList<pelanggan>();
        while (rs.next()) {
            pelanggan o = new pelanggan();
            o.setIdPelanggan(rs.getInt("idPelanggan"));
            list.add(o);
        }
        return list;
    }

    public String viewNama(String namaPelanggan) throws SQLException {
        String nama = null;
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT namaPenyewa FROM pelanggan WHERE idPelanggan = "+namaPelanggan+"");
        while(rs.next()){
            pelanggan o = new pelanggan();
            o.setNamaPelanggan(rs.getString("namaPenyewa"));
            nama = o.getNamaPelanggan();
        }
        return nama;
    }


}
